package com.atguigu.serviceedu.controller;


import com.aiguigu.common_utils.R;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.entity.vo.TeacherQuery;
import com.atguigu.serviceedu.service.EduTeacherService;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/serviceedu/edu-teacher")
//swagger 测试中的接口名称
@Api(tags = "讲师管理")
//@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;
    @ApiOperation("查看所有讲师")
    @GetMapping("findAll")
    public R finAllTeacher(){
        List<EduTeacher> list=eduTeacherService.list(null);
        return R.ok().data("item",list);
    }

    @DeleteMapping("{id}")
    @ApiOperation("逻辑删除讲师")
    public R removeTeacher(@PathVariable String id){
        Boolean flag=eduTeacherService.removeTeacherById(id);
        if(flag){
            return R.ok();
        }else {
            return R.error().message("删除失败,教师已经上线课程");
        }

    }
    @GetMapping("pageTeacher/{current}/{limit}")
    @ApiOperation("分页查询")
    public R pageTeacher(@PathVariable long current,@PathVariable long limit){
        Page<EduTeacher> pageTeacher=new Page<>(current,limit);
        //调用方法 底层封装 把分页所有数据封装到pageTeacher中
        eduTeacherService.page(pageTeacher,null);
        long total=pageTeacher.getTotal();//总记录数
        List<EduTeacher> records=pageTeacher.getRecords();//数据list
        return R.ok().data("total",total).data("rows",records);
    }
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    @ApiOperation("分页模糊查询")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //@RequestBody   前端使用json传递数据，把json数据封装到对应对象里面  要使用post方法
        //@ResponseBody  返回数据 返回json数据
        Page<EduTeacher> pageTeacher=new Page<>(current,limit);
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();

        String name = teacherQuery.getName();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        Integer level = teacherQuery.getLevel();
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }

        eduTeacherService.page(pageTeacher,wrapper);


        long total=pageTeacher.getTotal();
        List<EduTeacher> records=pageTeacher.getRecords();

        return R.ok().data("total",total).data("rows",records);
    }

    //添加讲师接口方法
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        return save?R.ok():R.error();

    }
    //根据id查询
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher eduTeacher=eduTeacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }
    //修改
    @PostMapping("updateTeacher")
    public R getTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = eduTeacherService.updateById(eduTeacher);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

